<?php
/**
 * PHP Template.
 */
class classUsersData{
    
    public function Insert($aasValues){//association array of strings = aas :)
        $sql = ' 
            INSERT INTO users (
                U_Organisation, U_Name, U_Surname, U_City, U_Street, U_PostID,
                U_Mobile, U_Phone, U_Email, U_BirthDate, U_ICO, U_DIC,
                U_Nick, U_Passwd
            )
            VALUES (
                "'.$aasValues['U_Organisation'].'",
                "'.$aasValues['U_Name'].'",
                "'.$aasValues['U_Surname'].'",
                "'.$aasValues['U_City'].'",
                "'.$aasValues['U_Street'].'",
                "'.$aasValues['U_PostID'].'",
                "'.$aasValues['U_Mobile'].'",
                "'.$aasValues['U_Phone'].'",
                "'.$aasValues['U_Email'].'",
                "'.$aasValues['U_BirthDate'].'",
                "'.$aasValues['U_ICO'].'",
                "'.$aasValues['U_DIC'].'",
                "'.$aasValues['U_Nick'].'",
                "'.sha1($aasValues['U_Passwd']).'"
           )
        ';
        mysql_query($sql);
        
        return mysql_insert_id();
    }
    
    public function Update($aasValues, $iUID){
        $sql = ' 
            UPDATE users 
            SET
                U_Organisation  = "'.$aasValues['U_Organisation'].'",
                U_Name          = "'.$aasValues['U_Name'].'",
                U_Surname       = "'.$aasValues['U_Surname'].'",
                U_City          = "'.$aasValues['U_City'].'",
                U_Street        = "'.$aasValues['U_Street'].'",
                U_PostID        = "'.$aasValues['U_PostID'].'",
                U_Mobile        = "'.$aasValues['U_Mobile'].'",
                U_Phone         = "'.$aasValues['U_Phone'].'",
                U_Email         = "'.$aasValues['U_Email'].'",
                U_BirthDate     = "'.$aasValues['U_BirthDate'].'",
                U_ICO           = "'.$aasValues['U_ICO'].'",
                U_DIC           = "'.$aasValues['U_DIC'].'"
            WHERE ID='.$iUID.'
        ';
        mysql_query($sql);
    }
    
    public function Update_Password($iUID, $sNewPassword){
        $sql = ' 
            UPDATE users 
            SET U_Passwd  = "'.sha1($sNewPassword).'"
            WHERE ID='.$iUID.'
        ';
        mysql_query($sql);
    }

        public function InsertDataToSession($aasValues){//association array of strings = aas :)
            $_user_sess = new classSessions();
            $_user_sess->Open(_SESS_USER_DATA_);

            $user_sess_value =
                'ID'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.'0'._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Organisation'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Organisation']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Name'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Name']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Surname'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Surname']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_City'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_City']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Street'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Street']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_PostID'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_PostID']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Mobile'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Mobile']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Phone'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Phone']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_Email'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_Email']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_BirthDate'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_BirthDate']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_ICO'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_ICO']._USER_TEMP_DATA_ITEM_DELIMITER_.
                'U_DIC'._USER_TEMP_DATA_INDEX_VALUE_DELIMITER_.$aasValues['U_DIC'];

            //////////////////////////////////
            if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/19.txt', $user_sess_value);
            //////////////////////////////////

            $_user_sess->serializeIt($user_sess_value);
    }
    
    public function Delete(){}

    public function DeleteSessionData(){
        $_user_sess = new classSessions();
        $_user_sess->Open(_SESS_USER_DATA_);
        $_user_sess->serializeIt('');
    }
    
    public function LoginCheck($sLogin,$sPasswd){
        $sql_result = mysql_query('SELECT COUNT(*) FROM users WHERE U_Nick=\''.$sLogin.'\' AND U_Passwd =\''.sha1($sPasswd).'\';');
        $sql_row    = mysql_fetch_row($sql_result);
        
        if($sql_row[0]>0){
          $sql_result = mysql_query('SELECT ID FROM users WHERE U_Nick=\''.$sLogin.'\' AND U_Passwd =\''.sha1($sPasswd).'\';');
          $sql_obj    = mysql_fetch_object($sql_result);
          return $sql_obj->ID;
        }
    }
    
    public function NickNameCheck($sNickanme){
        $sql_result = mysql_query('SELECT COUNT(*) FROM users WHERE U_Nick=\''.$sNickanme.'\';');
        $sql_row    = mysql_fetch_row($sql_result);    
        return $sql_row[0];         
    }
    
    
    public function PasswordCheck($iUID, $sPasswd){
        $sql_result = mysql_query('SELECT COUNT(*) FROM users WHERE ID='.$iUID.' AND U_Passwd =\''.sha1($sPasswd).'\';');
        $sql_row    = mysql_fetch_row($sql_result);  
        return $sql_row[0];         
    }


    public function geUserID($sNickanme){
        $sql_result = mysql_query('SELECT ID FROM users WHERE U_Nick=\''.$sNickanme.'\';');
        $sql_row    = mysql_fetch_object($sql_result);
        return $sql_row->ID;
    }
    
    
    public function getUserInfo($iUID=0){       
        $sql_result = mysql_query('SELECT * FROM users WHERE ID=\''.$iUID.'\';');
        $sql_obj    = mysql_fetch_object($sql_result);

        $aasValues['U_Organisation']  =$sql_obj->U_Organisation;
        $aasValues['U_Name']          =$sql_obj->U_Name;
        $aasValues['U_Surname']       =$sql_obj->U_Surname;
        $aasValues['U_City']          =$sql_obj->U_City;
        $aasValues['U_Street']        =$sql_obj->U_Street;
        $aasValues['U_PostID']        =$sql_obj->U_PostID;
        $aasValues['U_Mobile']        =$sql_obj->U_Mobile;
        $aasValues['U_Phone']         =$sql_obj->U_Phone;
        $aasValues['U_Email']         =$sql_obj->U_Email;
        $aasValues['U_BirthDate']     =$sql_obj->U_BirthDate;
        $aasValues['U_ICO']           =$sql_obj->U_ICO;
        $aasValues['U_DIC']           =$sql_obj->U_DIC;

        return $aasValues;
    }

    public function ForgottenPasswdDuplicityTest($iUserID){
        $sql_result = mysql_query('SELECT COUNT(*) FROM users_new_psswds WHERE user_id='.$iUserID);
        $sql_row = mysql_fetch_row($sql_result);
        return $sql_row[0];
    }

    public function ForgottenPasswd_SaveNewPassword($iUserID, $Date, $Identifier, $NewPassword){
        @$sql_result = mysql_query('
            INSERT INTO
                users_new_psswds (user_id, exp_date, identificator, new_password)
            VALUES
                ('.$iUserID.', "'.$Date.'", "'.$Identifier.'", "'.$NewPassword.'")
        ');
        return $sql_result;
    }
    
    public function ForgottenPasswd_DeleteNewPassword($iUserID){
        @$sql_result = mysql_query('
            DELETE FROM
                users_new_psswds
            WHERE
                user_id = '.$iUserID
        );
        return $sql_result;
    }

    public function ForgottenPasswd_DeleteAllExpiredNewPassword(){
        @$sql_result = mysql_query('
            DELETE FROM
                users_new_psswds
            WHERE
                exp_date  < NOW()'
        );
        return $sql_result;
    }


}
?>
